package com.wanxi.servlet;

import com.wanxi.model.ProductModel;
import com.wanxi.service.ProductService;
import com.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");

        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String enterprise = req.getParameter("enterprise");
        String sort = req.getParameter("sort");
        String introduce = req.getParameter("introduce");
        String style = req.getParameter("style");

        ProductModel productModel = ProductModel.builder()
                .id(Tool.strToInt(id))
                .name(name)
                .sort(Tool.strToInt(sort))
                .style(Tool.strToInt(style))
                .introduce(introduce)
                .enterprise(enterprise)
                .build();

        ProductService productService = new ProductService();
        productService.update(productModel);

        resp.sendRedirect("/findAll");
    }
}
